package com.fingard.dsp.bank.directbank.cmbc01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IFinanceRecInfo;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.ReqQryFinanceRecInfo;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.RetAtsFinanceRecInfo;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.RetAtsFinanceRecInfoDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

/**
 * 获取还款信息
 *
 * @author marlon
 * @date 2019/1/15
 */
@Component("CMBC01QryFinanceRecInfo")
public class CMBC01QryFinanceRecInfo extends CMBC01Base implements IFinanceRecInfo {
    @Override
    public RetAtsFinanceRecInfo qryFinanceRecInfo(ReqQryFinanceRecInfo qryFinanceRecInfo) throws Exception {
        RetAtsFinanceRecInfo retAts = new RetAtsFinanceRecInfo(qryFinanceRecInfo.reqHeader);
        retAts.writeStart();
        try {
            CMBC01Header bankHeader = new CMBC01Header();
            bankHeader.trnCode = "XR210006";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            XElement tmpXElement = new XElement("xDataBody", "");
            //特约店代码
            tmpXElement.addChild(new XElement("dealerCode", qryFinanceRecInfo.DealerCode));
            //车辆状态  I：在库 O：已赎
            tmpXElement.addChild(new XElement("vehicleStatus", "O"));
            //入库起始日
            tmpXElement.addChild(new XElement("inStartDate", Format.Date10Format.format(Format.Date8Format.parse(qryFinanceRecInfo.BeginDate))));
            //入库截止日
            tmpXElement.addChild(new XElement("inEndDate", Format.Date10Format.format(Format.Date8Format.parse(qryFinanceRecInfo.EndDate))));
            //出库起始日
            tmpXElement.addChild(new XElement("outStartDate", ""));
            //出库截止日
            tmpXElement.addChild(new XElement("outEndDate", ""));
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();//写入报文完成

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                    String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                    if ("0".equals(tmpRetCode)) {
                        retAts.writeRespCode("0");
                        retAts.writeRespInfo(tmpRetMessage);
                        while (tmpBkReader.read("/CMBC/xDataBody/Detail")) {
                            RetAtsFinanceRecInfoDetail tmpRetDetail = new RetAtsFinanceRecInfoDetail();
                            tmpRetDetail.DealerCode = qryFinanceRecInfo.DealerCode;
                            tmpRetDetail.VIN = tmpBkReader.readedXElement.getSingleText("vin");
                            tmpRetDetail.RepaymentDate = tmpBkReader.readedXElement.getSingleText("repaymentDate");
                            tmpRetDetail.RepaymentMoney = tmpBkReader.readedXElement.getSingleText("repaymentAmount");
                            tmpRetDetail.CarMortgageTime = tmpBkReader.readedXElement.getSingleText("inDate");
                            tmpRetDetail.Reserve1 = "";
                            tmpRetDetail.Reserve2 = "";
                            tmpRetDetail.Reserve3 = "";
                            tmpRetDetail.Reserve4 = "";
                            tmpRetDetail.Reserve5 = "";
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpRetMessage);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }

        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
        }
        retAts.writeEnd();
        return retAts;

    }
}


